package cn.itcast.tanhua.server.controller;

import cn.itcast.tanhua.common.utils.UserThreadLocal;
import cn.itcast.tanhua.server.service.TodayBestService;
import cn.itcast.tanhua.common.utils.Cache;
import cn.itcast.tanhua.server.vo.PageResult;
import cn.itcast.tanhua.server.vo.RecommendUserQueryParam;
import cn.itcast.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TodayBestController
 * @Description
 * @Author WU
 * @Date 2021/5/28 15:36
 * @Version 1.0
 */
@RestController
@RequestMapping("tanhua")
@Slf4j
public class TodayBestController {
    @Autowired
    private TodayBestService todayBestService;

    /**
    * @Description:今日佳人接口
    * @Param:
    * @return:
    * @Author: Mr.Tan
    * @Date: 2021/5/28
    */
    @GetMapping("todayBest")
    public ResponseEntity<TodayBest>  queryTodayBest(){
        try {
//        通过service层调用方法查询，接受结果后需要try 。。catch，捕捉service层可能抛出的异常，防止异常抛至前端
            TodayBest todayBest = this.todayBestService.queryTodayBest();
//            如果返回值不为空，则查询成功并将数据返回
            if (null != todayBest) {
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
//            捕捉到错误则记录日志
            log.error("查询今日佳人出错~ userId = "+ UserThreadLocal.get().getId());
        }

//        返回值为空则返回服务器错误的状态
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
    * @Description: 推荐列表接口
    * @Param: token/param
    * @return:
    * @Author: Mr.Tan
    * @Date: 2021/5/28
    */
    @GetMapping("recommendation")
    @Cache
    public ResponseEntity<PageResult> queryRecommendation( RecommendUserQueryParam param) {
        try {
            PageResult pageResult = this.todayBestService.queryRecommendation(param);
            if (null != pageResult) {
//                查询到的结果不为空则返回查到的数据
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
//            捕捉到异常则记录
            log.error("查询推荐用户列表出错~ userId = "+UserThreadLocal.get().getId(),e);
        }
//        查询到的结果为空则返回服务器错误的状态
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
